//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using ViNoToolkit;

public class ViNoLayerFactory : ISpriteFactory {
	static Mesh m_SharedMesh;
	static Material m_UnlitAlphaWithFade;	
	
	public override void OnSetActiveColor( GameObject obj ){
		// TODO : This is Slow.
		ColorPanel[] panels = obj.GetComponentsInChildren<ColorPanel>();
		for(int i=0;i<panels.Length;i++){
			ColorPanel panel = panels[ i ];
			panel.SetActiveColor();
		}
	}

	public override void OnSetDeactiveColor( GameObject obj ){
		// TODO : This is Slow.
		ColorPanel[] panels = obj.GetComponentsInChildren<ColorPanel>();
		for(int i=0;i<panels.Length;i++){
			ColorPanel panel = panels[ i ];
			panel.SetDeactiveColor();
		}
	}	

	static public GameObject Create( SceneData.SceneNodeData data ){
		// GO Getter.
		string nameCombParent = data.parentname + "/" + data.name;
//		Debug.Log( "Create :" + nameCombParent);
//		bool goFoundInScene = false;
		GameObject go = GameObject.Find( nameCombParent );
		if( go == null ){
			go = new GameObject( data.name );
			go.AddComponent<ThisObjectIsCached>();
		}
//		else{
//			goFoundInScene = true;
//		}

//		go.tag = data.nodeType.ToString();
		ViNoSceneNode sceneNodeComp = go.GetComponent<ViNoSceneNode>();
		if( sceneNodeComp == null ){
			sceneNodeComp = go.AddComponent<ViNoSceneNode>();
		}
		sceneNodeComp.makePixelPerfect = data.makePixelPerfect;
		if( ! data.show ){
			sceneNodeComp.activeAtStart = false;
		}

		GameObject parent = GameObject.Find( data.parentname );
		Vector3 pos = new Vector3( data.posX , data.posY , data.posZ );
		Vector3 rot = new Vector3( data.rotX , data.rotY , data.rotZ );
		Vector3 scale = new Vector3( data.sclX , data.sclY , data.sclZ );

		if( ! string.IsNullOrEmpty( data.texturePath ) ){
			Create( go , parent , pos , rot , scale , data.texturePath , data.alpha , data.makePixelPerfect );// , goFoundInScene );
		}
		else{
#if false
			go.transform.parent = parent.transform;
			go.transform.localScale = scale;
			TweenPosition.Begin( go , 1f , pos );
#else			

			ViNoGOExtensions.SetTransform( go , parent , pos , rot , scale );
#endif			
		}
		return go;
	}

	static public GameObject Create( GameObject go , GameObject parent , Vector3 pos , Vector3 rot , Vector3 scale , string texturePath , float alpha , bool makePixelPerfect ){//, bool goFoundInScene ){
		if( m_SharedMesh == null ){
			m_SharedMesh = Resources.Load ( "ViNoSimplePlaneMesh" , typeof(Mesh) ) as Mesh;
		}
			
		MeshFilter mf = go.GetComponent<MeshFilter>();
		if( mf == null ){
			mf = go.AddComponent<MeshFilter>();
			mf.mesh = m_SharedMesh;
		}
		
		MeshRenderer ren = go.GetComponent<MeshRenderer>();		
		if( ren == null ){
			ren = go.AddComponent<MeshRenderer>();		

			Shader shader = Resources.Load ( "Shader/Unlit Alpha" , typeof( Shader) ) as Shader;			
			Material newMat = new Material( shader );		
			ren.sharedMaterial = newMat;
		}

		ren.sharedMaterial.mainTexture = Resources.Load ( texturePath , typeof( Texture2D ) ) as Texture2D;

		ViNoSceneNode sceneNodeComp = go.GetComponent<ViNoSceneNode>();
		if( sceneNodeComp == null ){
			sceneNodeComp = go.AddComponent<ViNoSceneNode>();
		}		
		sceneNodeComp.currentLoadedTexturePath = texturePath;

		ColorPanel panel = go.GetComponent<ColorPanel>();
		if( panel == null ){
			panel = go.AddComponent<ColorPanel>();
			panel.meshRendererCollection = new MeshRenderer[ 1 ];
			panel.meshRendererCollection[ 0 ] = ren;
		}

			if( Application.isPlaying && SceneCreator._FADEIN_AT_CREATE ){
				Color col = ren.sharedMaterial.color;
//				if( ! goFoundInScene ){
					col.a = 0f;
//				}
				ren.sharedMaterial.color = col;
				panel.StartFade( alpha , col.a );
			}
//		}	
			
#if false
			go.transform.parent = parent.transform;
			go.transform.localScale = scale;
			TweenPosition.Begin( go , 1f , pos );
#else			
			if( ren.sharedMaterial.mainTexture != null && makePixelPerfect ){
				Texture tex = ren.sharedMaterial.mainTexture;
				scale.x = tex.width;
				scale.y = tex.height;
			}
			ViNoGOExtensions.SetTransform( go , parent , pos , rot , scale );
#endif			
		return go;
	}	

	public override GameObject Create( ref SceneData.SceneNodeData data , GameObject node , Vector3 pos , Vector3 scl , GameObject parent ){
		ViNoLayerFactory.Create( data );
		return node;
	}
				
	public override void SaveData( ref SceneData.SceneNodeData dat ,  GameObject obj  ){
		base.SaveData( ref dat , obj );
				
		// Serialize Sprite Data .
		MeshRenderer meshRenderer = obj.GetComponent<MeshRenderer>();
		if( meshRenderer  != null ){
			dat.nodeType = SceneNodeType.LEAF;
			dat.uiAtlasName = "";		
			dat.alpha = meshRenderer.sharedMaterial.color.a;
			dat.show = meshRenderer.enabled;
		}		
		else{
			ColorPanel p =  obj.GetComponent<ColorPanel>();
			dat.nodeType = SceneNodeType.NODE;
			if( p != null ){			
				dat.alpha = p.alpha;
			}
			else{
				dat.alpha = 1f;				
			}
			dat.show = true;
		}				
		
		ViNoSceneNode vs = obj.GetComponent<ViNoSceneNode>();		
		if( vs != null ){					
			vs.singleNodeData = dat;	
			dat.makePixelPerfect = vs.makePixelPerfect;
			if( meshRenderer  != null ){
				dat.texturePath = vs.currentLoadedTexturePath;			
			}
		}			
	}
	
	public override void LoadData( ref SceneData.SceneNodeData data ,  GameObject obj ){
		Create( data );		
	}

	public override void PreFadeoutAndDestroy( GameObject root ){
		ColorPanel[] panels = root.GetComponentsInChildren<ColorPanel>();
		for( int i=0;i<panels.Length;i++){
			panels[ i ].StartFade( 0f , panels[ i ].alpha );
		}			
	}

	public override void OnFadeoutAndDestroy( GameObject root ){
		// Destroy Created Materials to avoid Leaking .
		MeshRenderer[] rens = root.GetComponentsInChildren<MeshRenderer>();
		if( rens != null ){
			for( int i=0;i<rens.Length;i++){
				DestroyImmediate( rens[ i ].sharedMaterial );
			}
		}		
	}	

}